<?php
/**
 * Created by PhpStorm.
 * User: 王文凡<787802868@qq.com>
 * Date: 2016/8/25 0025
 * Time: 17:52
 */

namespace Common\Model;

use Think\Model;

class MaterialModel extends Model
{
    protected $_validate = [
        ['m_name', 'require', '货品名称不能为空'],
        ['m_name', '', '此货品已存在！', 0, 'unique', 1],
        ['bracode', 'require', '请填写货品编码'],
        ['bracode', '', '此货品编码已存在！', 0, 'unique', 1],
        ['m_number', 'require', '请填写条形编码'],
        ['m_number', '', '此条形编码已存在！', 0, 'unique', 1],
        ['standard', 'require', '请填写商品规格！'],
        ['bid_price', 'require', '请填写进货价格！'],
        ['sell_price', 'require', '请填写售价！'],
        ['expiration_date', 'require', '保质期必须填写！'],
        ['darr', 'require', '请选择计量单位！'],
        ['sping', 'require', '请选择存储仓位！'],

    ];

    //新增货品
    public function newAdd($mid='')
    {

        if(!$this->create()){
            $arr['state']='error';
            $arr['info']=$this->getError();
            return $arr;
        }else{
        if(empty($mid)){
            if($this->add()){
                $arr['state']='success';
                $arr['info']='添加成功';
            }else{
                $arr['state']='error';
                $arr['info']='添加失败';
            }
            return $arr;
        }else{
            if($this->where("mid=%d",$mid)->save($_POST)){
                $arr['state']='success';
                $arr['info']='修改成功';
            }else{
                $arr['state']='error';
                $arr['info']='添加失败';
            }
            return $arr;
        }
        }
    }
    //货品查询
    public function kindsList($mid='')
    {

        if(empty($mid)){
            $counts=$this->where("status=%s",1)->count();
            $page=page($counts,15,5);
            $list=$this->alias("a")->field("a.*,b.s_name,c.d_name")->join("sr_shipping as b on a.entrepot=b.sid")
                ->join("sr_danwei as c on a.unit=c.did")
                ->where("a.status=%d",1)->order("a.mid DESC")->limit($page->firstRow, $page->listRows)->select();
            $arr['list']=$list;
            $arr['page']=$page;
            return $arr;
        }else{
           $result= $this->alias("a")->field("a.*,b.s_name,c.d_name")->join("sr_shipping as b on a.entrepot=b.sid")
                ->join("sr_danwei as c on a.unit=c.did")
                ->where("a.mid=%d",$mid)->find();
            return $result;
        }
    }

    //货品搜索
    public function searchKinds($keywords)
    {
        if($keywords ==null){
            $arr['state']='error';
            $arr['info']='请输入关键词';
            return $arr;
        }else{
            $where['bracode|m_name']=$keywords;
            $re=$this->field("mid,m_name,bracode,standard,m_number,bid_price,sell_price")->where($where)->find();
            if($re){
                $arr['state']='success';
                $arr['info']=$re;
            }else{
                $arr['state']='error';
                $arr['info']='没有此货品';
            }
            return $arr;
        }


    }
}

















